﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myprofile.aspx.cs" Inherits="myprofile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ import Namespace="RobozzleModel" %>
<%@ import Namespace="System.Linq" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="../stats.css" rel="stylesheet" type="text/css"/>
    <title>RoboZZle User Profile</title>
</head>
<body>
    <div id="Div1" runat="server">
    <%= StatsCommon.MenuBar(MenuPage.None)%>
    <%
        string userArg = Request["user"];
        string passwordArg = Request["password"];
        string passwordHashArg = Request["passwordhash"];
        string updateArg = Request["update"];

        if (passwordHashArg == null && passwordArg != null) passwordHashArg = StatsCommon.HashPassword(passwordArg);
        
        bool foundUser = false;
        string errorMsg = null;
        string successMsg = null;

        if (updateArg != null)
        {
            string emailArg = Request["email"];
            string aboutArg = Request["about"];
            string websiteArg = Request["website"];
            
            RobozzleEntities entities = new RobozzleEntities();
            User user = entities.User.Where(r => r.Username == userArg && r.Password == passwordHashArg).FirstOrDefault();

            if (user != null)
            {
                user.Email = emailArg;
                user.About = aboutArg;
                user.Website = websiteArg;
                entities.SaveChanges();
                successMsg = "Profile successfully updated.";
            }
            else
            {
                errorMsg = "Profile could not be found.";
            }
        }

        if (userArg != null && passwordHashArg != null) 
        {
            RobozzleEntities entities = new RobozzleEntities();
            User user = entities.User.Where(r => r.Username == userArg && r.Password == passwordHashArg).FirstOrDefault();
            if (user != null)
            {
                foundUser = true;
                %>
                    <h1>RoboZZle User Profile for <%= user.Username%></h1>

                    <%
                        if (successMsg != null)
                        {
                            Response.Write(
                                "<p class=\"successMsg\">" + Server.HtmlEncode(successMsg)
                                + " <a href=\"/user.aspx?name=" + user.Username + "\">View the profile</a>"
                                + "</p>");
                        }
        if (errorMsg != null) Response.Write("<p class=\"errorMsg\">" + Server.HtmlEncode(errorMsg) + ".</p>");
                    %>
                    
                     <form action="" method="post">
                        <input type="hidden" name="passwordhash" value="<%= passwordHashArg %>" />
                        <input type="hidden" name="user" value="<%= userArg %>" />
                        <input type="hidden" name="update" value="1" />
                        <div class="forumComment" style="width:300px">
                            <table style="width:300px">
                                <tr>
                                    <td style="width:60px;text-align: left;">Email:</td>
                                    <td style="text-align:left"><input name="email" type="text" style="width:200px" value="<%= Server.HtmlEncode(user.Email) %>" maxlength="250"/></td>
                                </tr>
                                
                                <tr>
                                    <td colspan="2"><div style="margin-top:10px; margin-bottom:10px">If you have a <a href="http://en.gravatar.com">gravatar</a> associated with your email address, the gravatar will be shown in your profile.</div></td>
                                </tr>                                                                
                                
                                <tr>
                                    <td style="width:60px;text-align: left;">About:</td>
                                    <td style="text-align:left"><input name="about" type="text" style="width:200px" value="<%= Server.HtmlEncode(user.About) %>" maxlength="450"/></td>
                                </tr>                                
                                
                                <tr>
                                    <td style="width:60px;text-align: left;">Website:</td>
                                    <td style="text-align:left"><input name="website" type="text" style="width:200px" value="<%= Server.HtmlEncode(user.Website) %>" maxlength="150"/></td>
                                </tr>
                                                          
                                <tr>
                                    <td colspan="2"><input type="submit" value="Update" /></td>
                                </tr>
                            </table>              
                        </div>
                    </form>                 
                
                <%
        }
            else
            {
                errorMsg = "Invalid username / password";
            }
        }
        if (!foundUser)
        {
    %>
        <h1>RoboZZle User Profile</h1>
        <%
            if (errorMsg != null) Response.Write("<p class=\"errorMsg\">" + Server.HtmlEncode(errorMsg) + ".</p>");
        %>
        <p>To edit your profile, please enter your username and password:</p>
        <form action="" method="post">
            <div class="forumComment" style="width:220px">
                <table style="width:220px">
                    <tr>
                        <td style="width:60px;text-align: left;">User:</td>
                        <td style="text-align:left"><input name="user" type="text" style="width:100px" value="<%= Server.HtmlEncode(userArg) %>"/></td>
                    </tr>
                    <tr>
                        <td style="width:60px;text-align: left;">Password:</td>
                        <td><input name="password" type="password" style="width:100px"/></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" value="Continue" /></td>
                    </tr>
                </table>              
            </div>
        </form>
        <p><a href="/forgotpassword.aspx">Lost password</a></p>
    <%
        }
    %>
    <%= StatsCommon.Footer() %>
    </div>
</body>
</html>
